<template>
  <div class="m-switch-wrap">
    <div @click="disabled ? e => e.preventDefault() : onSwitch()" :class="['m-switch', { 'switch-checked': isChecked, 'disabled': disabled }]">
      <div :class="['u-switch-inner', isChecked ? 'inner-checked' : 'inner-unchecked' ]">{{ isChecked ? checkedInfo : uncheckedInfo }}</div>
      <div :class="['u-node', { 'node-checked': isChecked }]"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CustomSwitch',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    checkedInfo: {
      type: [Number, String],
      default: null
    },
    uncheckedInfo: {
      type: [Number, String],
      default: null
    },
    disabled: {
      type: Boolean,
      default: false
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    isChecked() {
      return this.value;
    }
  },
  methods: {
    onSwitch () {
      if (this.disabled) return;
      this.$emit('change', !this.value);
    }
  }
}
</script>

<style lang="less" scoped>
@themeColor: #1890FF;
.m-switch-wrap {
  height: 22px;
  min-width: 44px;
  display: inline-block;
  .m-switch {
    position: relative;
    height: 22px;
    color: rgba(0,0,0,.65);
    font-size: 14px;
    background: rgba(0,0,0,.25);
    border-radius: 100px;
    cursor: pointer;
    transition: background .36s;
    .u-switch-inner {
      display: inline-block;
      color: #fff;
      font-size: 14px;
      line-height: 22px;
      padding: 0 8px;
      transition: all .36s;
    }
    .inner-checked {
      margin-right: 18px;
    }
    .inner-unchecked {
      margin-left: 18px;
    }
    .u-node {
      position: absolute;
      top: 2px;
      left: 2px;
      width: 18px;
      height: 18px;
      background: #FFF;
      border-radius: 100%;
      cursor: pointer;
      transition: all .36s;
    }
    .node-checked {
      left: 100%;
      margin-left: -2px;
      transform: translateX(-100%);
    }
  }
  .switch-checked {
    background: @themeColor;
  }
  .disabled {
    cursor: not-allowed;
    opacity: .4;
  }
}
</style>
